package com.lingxinst.farm.base.ext

import androidx.activity.ComponentActivity
import androidx.lifecycle.LiveData
import com.lynn.common.dialog.BaseDialog
import com.lynn.common.dialog.createHttpLoadingDialog

/**
 * @description: 自动绑定生命周期的扩展类
 * @author: bailing
 * @datetime: 2025/6/5 08:58
 **/

/**
 * 通过 LifecycleObserver 确保加载动画在界面销毁时自动隐藏
 */
fun ComponentActivity.showLoadingWithLifecycle(loadingLiveData: LiveData<Boolean>) {
    val dialog: BaseDialog by lazy { createHttpLoadingDialog(this) }
    loadingLiveData.observe(this) { isShow ->
        if (isShow) {
            dialog.show()
        } else {
            dialog.dismiss()
        }
    }
}